<html>
<head><meta charset="utf-8"><title>why does io::Empty need a constructor? · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/why.20does.20io.3A.3AEmpty.20need.20a.20constructor.3F.html">why does io::Empty need a constructor?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="194682623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/why%20does%20io%3A%3AEmpty%20need%20a%20constructor%3F/near/194682623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/why.20does.20io.3A.3AEmpty.20need.20a.20constructor.3F.html#194682623">(Apr 20 2020 at 14:30)</a>:</h4>
<p>Hi, <br>
I found <code>io::Empty</code> useful in my tests, but I can't understand why I need to call <code>io::empty()</code> instead of just <code>Empty</code><br>
(ie <code>empty().read(&amp;mut [0u8])</code> vs <code>Empty.read(&amp;mut [0u8])</code>).</p>
<p>Should I PR a change for this?(it's not a breaking change)<br>
I find all the constructors in std::io weird (why <code>io::snik()</code> instead of <code>std::io::Sink::new()</code>?) butthe empty in particular, because the only reason you can construct it yourself is because of the <code>_priv: ()</code> field.</p>



<a name="194697989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/why%20does%20io%3A%3AEmpty%20need%20a%20constructor%3F/near/194697989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/why.20does.20io.3A.3AEmpty.20need.20a.20constructor.3F.html#194697989">(Apr 20 2020 at 16:12)</a>:</h4>
<p>if nothing else, it's a stronger API commitment to let a type be constructed with no fields -- then there can _never_ be any fields added, not even a <code>PhantomData</code>, <code>PhantomPinned</code>, or something new like that in the future</p>



<a name="194698042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/why%20does%20io%3A%3AEmpty%20need%20a%20constructor%3F/near/194698042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/why.20does.20io.3A.3AEmpty.20need.20a.20constructor.3F.html#194698042">(Apr 20 2020 at 16:13)</a>:</h4>
<p><del>and in fact, <code>Empty&lt;T&gt;</code> needs a <code>PhantomData&lt;T&gt;</code>, because you can't have an unused type parameter</del><br>
(scratch that, you're talking about <code>io::Empty</code>, not <code>iter::Empty</code>)</p>



<a name="194698220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/why%20does%20io%3A%3AEmpty%20need%20a%20constructor%3F/near/194698220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/why.20does.20io.3A.3AEmpty.20need.20a.20constructor.3F.html#194698220">(Apr 20 2020 at 16:14)</a>:</h4>
<p>Yeah but it's empty by definition...</p>



<a name="194698268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/why%20does%20io%3A%3AEmpty%20need%20a%20constructor%3F/near/194698268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/why.20does.20io.3A.3AEmpty.20need.20a.20constructor.3F.html#194698268">(Apr 20 2020 at 16:15)</a>:</h4>
<p>and adding a type parameter / lifetime in the future will be a breaking change</p>



<a name="194698486"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/why%20does%20io%3A%3AEmpty%20need%20a%20constructor%3F/near/194698486" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/why.20does.20io.3A.3AEmpty.20need.20a.20constructor.3F.html#194698486">(Apr 20 2020 at 16:17)</a>:</h4>
<p>I think you could add a type parameter with a suitable default</p>



<a name="194698713"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/why%20does%20io%3A%3AEmpty%20need%20a%20constructor%3F/near/194698713" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/why.20does.20io.3A.3AEmpty.20need.20a.20constructor.3F.html#194698713">(Apr 20 2020 at 16:19)</a>:</h4>
<p>maybe <code>PhantomPinned</code> is a better example, not that this needs it, but it's notable because that came around long after <code>io::Empty</code>. suppose there could be some new <code>PhantomFoo</code> that would be useful here later, but we can't add it if we're publicly bound to having no fields</p>



<a name="194704106"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/why%20does%20io%3A%3AEmpty%20need%20a%20constructor%3F/near/194704106" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/why.20does.20io.3A.3AEmpty.20need.20a.20constructor.3F.html#194704106">(Apr 20 2020 at 16:59)</a>:</h4>
<p>Ha, that's a good example, you're right. <br>
I still find the current io constructors weird but you're right about maybe some weird new concept in rust's future can require Empty to change</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>